#include "threshold_sensor.hpp"
#include "discrete_sensor.hpp"
#include <iostream>
#include <unordered_map>
#include <limits>
#include <utility>

using namespace my_cpp_project;


int add_discrete_sensor(int id, bool status, std::unordered_map<int, my_cpp_project::DiscreteSensor> &sensors);
// int add_threshold_sensor(int id, double temp, std::unordered_map<int, my_cpp_project::Sensor> &sensors);
// int query_sensor_by_id(int id, std::unordered_map<int, my_cpp_project::Sensor> &sensors);

// int query_discrete_sensor_by_id(int id, std::unordered_map<int, my_cpp_project::DiscreteSensor> &sensors);

// int delete_sensor(int id, std::unordered_map<int, my_cpp_project::Sensor> &sensors);
int delete_discrete_sensor(int id, std::unordered_map<int, my_cpp_project::DiscreteSensor> &sensors);

// void print_all_sensors_info(std::unordered_map<int, my_cpp_project::Sensor> &sensors);

void print_all_discrete_sensors_info(std::unordered_map<int, my_cpp_project::DiscreteSensor> &sensors);

int main()
{
    // std::unordered_map<int, my_cpp_project::Sensor> sensors;
    std::unordered_map<int, DiscreteSensor> discrete_sensors; // 对象数组

    int operate;

    do
    {
        std::cout << "请选择操作:" << std::endl;
        std::cout << "1.新增离散传感器" << std::endl;
        // std::cout << "2.新增门限传感器" << std::endl;
        std::cout << "3.删除传感器" << std::endl;
        // std::cout << "4.打印所有传感器信息" << std::endl;
        std::cout << "4.打印所有离散传感器信息" << std::endl;
        // std::cout << "5.查询单个传感器信息" << std::endl;
        std::cout << "5.查询单个离散传感器信息" << std::endl;
        std::cout << "6.退出" << std::endl;
        std::cin >> operate;
        int id;
        bool status;
        // double temp;
        std::cout << "=====operate is " << operate << std::endl;
        switch (operate)
        {
        case 1:
            std::cout << "请输入离散传感器id，类型为int" << std::endl;
            std::cin >> id;
            std::cout << "请输入离散传感器状态，类型为bool" << std::endl;
            std::cin >> status;
            add_discrete_sensor(id, status, discrete_sensors);
            break;
            /*
                    case 2:
                        std::cout << "请输入门限传感器id，类型为int" << std::endl;
                        std::cin >> id;
                        std::cout << "请输入门限传感器读数，类型为double" << std::endl;
                        std::cin >> temp;
                        add_threshold_sensor(id, temp, sensors);
                        break;
            */

        case 3:
            std::cout << "请输入传感器id，类型为int，以删除传感器" << std::endl;
            std::cin >> id;
            delete_discrete_sensor(id, discrete_sensors);
            break;
        /*
                case 4:
                    std::cout << "以下为全部传感器信息" << std::endl;
                    print_all_sensors_info(sensors);
                    break;
        */
        case 4:
            std::cout << "以下为全部离散传感器信息" << std::endl;
            print_all_discrete_sensors_info(discrete_sensors);
            break;

        case 5:
            std::cout << "请输入需要查询的传感器编号" << std::endl;
            std::cin >> id;
            //query_discrete_sensor_by_id(id, discrete_sensors);
            break;

        case 6:
            break;

        default:
            std::cout << "输入错误，请重新输入" << std::endl;
            break;
        }
    } while (operate != 6);
}

/*
 int add_discrete_sensor(int id, bool status, std::unordered_map<int, my_cpp_project::Sensor> &sensors)
{
    if (sensors.find(id) != sensors.end())
    {
        std::cout << "已存在编号为" << id << "的传感器" << std::endl;
        return -1;
    }
    else
    {
        std::cout<<"====xxxxsize is "<<sensors.size()<<std::endl;
        my_cpp_project::DiscreteSensor sensor(id, 1, status);
        sensors.insert({id, sensor});
        std::cout<<"====yyyysize is "<<sensors.size()<<std::endl;
    }
    return 0;
}

 */

int add_discrete_sensor(int id, bool status, std::unordered_map<int, my_cpp_project::DiscreteSensor> &sensors)
{
    if (sensors.find(id) != sensors.end())
    {
        std::cout << "已存在编号为" << id << "的传感器" << std::endl;
        return -1;
    }
    else
    {
        my_cpp_project::DiscreteSensor cur_sensor(id, status);
        sensors.emplace(id, cur_sensor);
    }
    return 0;
}

int delete_discrete_sensor(int id, std::unordered_map<int, my_cpp_project::DiscreteSensor> &sensors)
{
    if(sensors.find(id) == sensors.end()){
        std::cout<<"不存在该传感器"<<std::endl;
        return -1;
    }else{
        sensors.erase(id);
        return 0;
    }
}

void print_all_discrete_sensors_info(std::unordered_map<int, my_cpp_project::DiscreteSensor> &sensors)
{
    for(auto it = sensors.begin(); it != sensors.end(); ++it)
    {
        it->second.print_all_info();
    }
}

